<?php

class FB_Widgets extends WP_Widget {
    
    public function __construct(){
        parent::__construct(
            'um_fb_widget',
            'Urban Meandering Facebook Like Widget',
            array('description' => 'A facebook widget ') );
        
    }
    
    public function widget($args, $instance)
    {
        extract($args);
       
        $this->widget_title = apply_filters('widget_title', $instance['title']);
        $this->facebook_id = $instance['app_id'];
        $this->facebook_username = $instance['page_name'];
        $this->facebook_width = $instance['width'];
        $this->facebook_show_faces = ($instance['show_faces'] == "1") ? "true" : "false";
        $this->facebook_stream = ($instance['show_stream'] == "1") ? "true" : "false";
        $this->facebook_header = ($instance['show_header']  == "1") ? "true" : "false";
        //echo '<pre>'; print_r($this); echo '</pre>';
        add_action('wp_footer', array(&$this, 'add_js'));
        
        if ( $this->widget_title )
        {
            echo $this->widget_title;
        }
        
				
        ?>
        <div class="fb-like-box"
             data-href="http://www.facebook.com/<?php echo $this->facebook_username; ?>"
             data-width="<?php echo $this->facebook_width; ?>"
             data-show-faces="<?php echo $this->facebook_show_faces; ?>" 
             data-stream="<?php echo $this->facebook_stream; ?>"
             data-header="<?php echo $this->facebook_header; ?>" >
        </div>
<?php
    }
    
/** 
 * Add Facebook javascripts 
 */  
public function add_js() {  
    echo '<div id="fb-root"></div> 
        <script>(function(d, s, id) { 
            var js, fjs = d.getElementsByTagName(s)[0]; 
            if (d.getElementById(id)) return; 
            js = d.createElement(s); js.id = id; 
            js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId='.$this->facebook_id.'"; 
            fjs.parentNode.insertBefore(js, fjs); 
        }(document, \'script\', \'facebook-jssdk\'));</script>';  
}
    
    public function update($new_instance, $old_instance)
    {
        $instance = array();
        $instance['title'] = strip_tags( $new_instance['title'] );  
        $instance['app_id'] = strip_tags( $new_instance['app_id'] );  
        $instance['page_name'] = strip_tags( $new_instance['page_name'] );  
        $instance['width'] = strip_tags( $new_instance['width'] );  

        $instance['show_faces'] = (bool) isset($new_instance['show_faces']) ? $new_instance['show_faces'] : 0;  
        $instance['show_stream'] = (bool) isset($new_instance['show_stream']) ? $new_instance['show_stream'] : 0;  
        $instance['show_header'] = (bool) isset($new_instance['show_header']) ? $new_instance['show_header']: 0;  
  
   
        return $instance;
    }
    
    public function form($instance)
    {
       
       $defaults = array(
          'title' => ( isset($this->widget_title) ) ? $this->widget_title :'',
          'app_id' => (isset($this->facebook_id)) ? $this->facebook_id : '',
          'page_name' => (isset($this->facebook_username)) ? $this->facebook_username : '',
          'width' => (isset($this->facebook_width)) ? $this->facebook_width : '',
          'show_faces' => (isset($this->facebook_show_faces)) ?  $this->facebook_show_faces : '',
          'show_stream' => (isset($this->facebook_stream)) ? $this->facebook_stream : '',
          'show_header' => (isset($this->facebook_header)) ? $this->facebook_header : ''
           
        );
       
       $instance = wp_parse_args((array) $instance, $defaults );
       
//        if ( isset( $instance[ 'title' ] ) ) {  
//            $title = $instance[ 'title' ];  
//        }  
//        else {  
//            $title = __( 'New title', 'text_domain' );  
//        }  
        ?>  
        <!-- Widget Title: Text Input -->
        <p><label for="<?php echo $this->get_field_id('title'); ?>"> Title </label>
           <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
                  name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" /> 
        </p>
        
        <!-- App Id: Text Input -->
        <p><label for="<?php echo $this->get_field_id('app_id'); ?>"> App Id </label>
           <input type="text" class="widefat" id="<?php echo $this->get_field_id('app_id'); ?>"
                  name="<?php echo $this->get_field_name('app_id'); ?>" value="<?php echo $instance['app_id']; ?>" />
        </p>
        
        <!-- Page name: Text Input -->
        <p><label for="<?php echo $this->get_field_id('page_name');?>"> Page Name  (http://www.facebook.com/[page_name]) </label>
           <input type="text" class="widefat" id="<?php echo $this->get_field_id('page_name'); ?>" 
                  name="<?php echo $this->get_field_name('page_name'); ?>" value="<?php echo $instance['page_name']; ?>" />
        </p>
        
        <!-- Width: Text Input -->
        <p><label for="<?php echo $this->get_field_id('width'); ?>"> Width </label>
           <input type="text" class="widefat" id="<?php echo $this->get_field_id('width'); ?>"
                  name="<?php echo $this->get_field_name('width'); ?>" value="<?php echo $instance['width']; ?>" />
        </p>
        <!-- Show Faces: Checkbox -->
        <p><label for="<?php echo $this->get_field_id('show_faces'); ?>"> Show Faces </label>
           <input type="checkbox" class="widefat" id="<?php echo $this->get_field_id('show_faces'); ?>" 
                  name="<?php echo $this->get_field_name('show_faces'); ?>" value="1" <?php echo ($instance['show_faces'] == 'true' ? "checked='checked'" : "" ); ?> />
        </p>
        <!-- Show Stream: -->
        <p><label for="<?php echo $this->get_field_id('show_stream'); ?>"> Show Stream </label>
           <input type="checkbox" class="widefat" id="<?php echo $this->get_field_id('show_stream'); ?>"
                  name="<?php echo $this->get_field_name('show_stream'); ?>" value="1" <?php echo ( $instance['show_stream'] == 'true' ? "checked='checked'" : '' ); ?> />
        </p>
        <!-- Show Header: Checkbox -->
        <p><label for="<?php echo $this->get_field_id('show_header'); ?>"> Show Header </label>
           <input type="checkbox" class="widefat" id="<?php echo $this->get_field_id('show_header'); ?>" 
                  name="<?php echo $this->get_field_name('show_header'); ?>" value="1" <?php echo ($instance['show_header'] == 'true' ? "checked='checked'" : ""); ?> />
        </p>
        <?php  
      
    }
}
add_action( 'widgets_init', create_function( '', 'register_widget("FB_Widgets");' ) );